package net.example.lesson5.copyfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamCopy {

    public void copyFileByStream(File src, File dst){
        try(InputStream is = new FileInputStream(src);
            OutputStream os = new FileOutputStream(dst);){
            byte[] buffer = new byte[10240];
            int length;
            while ((length = is.read(buffer)) > 0){
                os.write(buffer, 0 ,length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        StreamCopy copy = new StreamCopy();
        copy.copyFileByStream(new File(System.getProperty("user.dir")+"\\file\\cargo.war"),
                new File(System.getProperty("user.dir")+"\\file\\cargo2.war"));
        System.out.println("耗时："+(System.currentTimeMillis()-t1));
    }
}
